//
//  TTRecyclingListTwoViewController.m
//  TaoTuo
//
//  Created by LG on 2020/8/28.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTRecyclingListTwoViewController.h"
#import <JXCategoryTitleView.h>
#import <JXCategoryIndicatorLineView.h>
#import <JXCategoryListContainerView.h>
#import "TTRecyclingListTwoSubViewController.h"

@interface TTRecyclingListTwoViewController ()<JXCategoryViewDelegate, JXCategoryListContainerViewDelegate>

@property (nonatomic, strong) JXCategoryTitleView *categoryView;
@property (nonatomic, strong) JXCategoryListContainerView *listContainerView;
@property (nonatomic, strong) NSArray *titles;
/// 已经设置过数字了
@property (nonatomic, assign) BOOL isHasCount;

@end

@implementation TTRecyclingListTwoViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.isHasCount = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的在售";
    // 初始化JXCategoryTitleView
    self.titles = @[@"在售中", @"已售出", @"已下架"];
    self.categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 54)];
    self.categoryView.delegate = self;
    self.categoryView.titleNumberOfLines = 2;
    self.categoryView.titleColor = UIColorHex(333333);
    self.categoryView.titleSelectedColor = UIColorHex(333333);
    self.categoryView.titleFont = [UIFont systemFontOfSize:15];
    self.categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:15];
    self.categoryView.titles = self.titles;
    self.categoryView.titleColorGradientEnabled = YES;
    self.categoryView.contentEdgeInsetLeft = 45;        // 左边距
    self.categoryView.contentEdgeInsetRight = 45;       // 右边距
    [self.view addSubview:self.categoryView];
    
    // 添加指示器
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    lineView.indicatorColor = UIColorHex(9B71FF);
    lineView.indicatorHeight = 8;
    lineView.verticalMargin = 5;
    lineView.indicatorCornerRadius = 4;
    self.categoryView.indicators = @[lineView];
    
    
    // 初始化JXCategoryListContainerView并关联到categoryView
    self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    self.listContainerView.frame = CGRectMake(0, self.categoryView.bottom, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavBar_Height - self.categoryView.bottom);
    [self.view addSubview:self.listContainerView];
    //关联到categoryView
    self.categoryView.contentScrollView = self.listContainerView.scrollView;
    
//    self.categoryView.defaultSelectedIndex = self.defaultIndex;
//    self.listContainerView.defaultSelectedIndex = self.defaultIndex;
    
    /// 设置顶部统计数字
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setTopGoodsSellCountNotify:) name:@"setTopGoodsSellCountNotify_key" object:nil];
    
}

- (void)setTopGoodsSellCountNotify:(NSNotification *)notify
{
    if (self.isHasCount) {
        return;
    }
    NSDictionary *dic = notify.object;
    NSArray *arr = @[dic[@"0"], dic[@"1"], dic[@"2"]];
    NSMutableArray *newTitles = [NSMutableArray array];
    for (int i = 0; i < self.titles.count; i++) {
        NSString *title = [NSString stringWithFormat:@"%@\n%@", arr[i], self.titles[i]];
        [newTitles addObject:title];
    }
    self.categoryView.titles = newTitles;
    [self.categoryView reloadData];
    
    self.isHasCount = YES;
}


#pragma mark - JXCategoryViewDelegate
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index {
    //侧滑手势处理
    self.navigationController.interactivePopGestureRecognizer.enabled = (index == 0);

}

- (void)categoryView:(JXCategoryBaseView *)categoryView didScrollSelectedItemAtIndex:(NSInteger)index {
//    NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}



#pragma mark - JXCategoryListContainerViewDelegate
- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    TTRecyclingListTwoSubViewController *listVC = [[TTRecyclingListTwoSubViewController alloc] initWithStatus:index];
    return listVC;
}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}


- (UIView *)listView
{
    return self.view;
}


@end
